<template>
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加角色-->
      <el-col :span="3">
        <el-button type="primary" @click="addUserdialogVisible=true">添加角色</el-button>
      </el-col>
      <!-- 用户列表 start-->
      <el-table
          :data="roleList"
          style="width: 100%"
          border stripe>
        <!--展开列 此角色的权限-->
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row :class="['backLineBottom','elrow','vcenter']" v-for="(item1,i1) in scope.row.child">
              <!--一级权限-->
              <el-col :span="6">
                <el-tag>{{ item1.menuName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <el-row :class="[i2===0?'':'backLineTop']" v-for="(item2,i2) in item1.child">
                  <!-- 二级权限-->
                  <el-col :span="8">
                    <el-tag type="success">{{ item2.menuName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限-->
                  <el-col :span="16" :class="['backLineTop']">
                    <el-tag closable @close="removeAuthById(scope.row,item3.id)" type="warning"
                            v-for="(item3,i3) in item2.child">{{ item3.menuName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column
            prop="rolename"
            label="角色名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="describes"
            label="角色描述"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            width="180">
          <template v-slot="scope">
            {{ scope.row.createTime | fmtdate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-tooltip class="item" effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" circle></el-button>
              >
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="分配权限" placement="top" :enterable="false">
              <el-button type="success" icon="el-icon-check" circle @click="setRightDialog(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 用户列表 end-->
      <!--分配权限对话框-->
      <el-dialog
          title="分配权限"
          :visible.sync="rightDialogVisible"
          width="50%">
        <!--树状控件-->
        <el-tree ref="treeRef" :data="authList" :props="defaultProps" show-checkbox default-expand-all node-key="id"
                 :default-checked-keys="defKeys"></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="rightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "RoleList",
  data() {
    return {
      //角色列表
      roleList: [],
      //是否显示分配权限对话框
      rightDialogVisible: false,
      //全部权限列表
      authList: [],
      //树形控件参数
      defaultProps: {
        children: 'child',
        label: 'menuName'
      },
      //默认选中的树节点
      defKeys: [],
      //待分配权限的角色对象
      allotRole: {}
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    /*获取角色列表*/
    async getRoleList() {
      const {data: res} = await this.$axios.get("role/list")
      if(res.code!='0'){
        return this.$message.error(res.msg)
      }
      this.roleList = res.data
    },
    /*删除某个角色的某个权限*/
    removeAuthById(role, menuid) {
      this.$confirm('删除此权限，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data: res} = await this.$axios.delete(`role/delRoleRight/${role.id}/${menuid}`)
        role.children = res.data
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    /*显示分配权限对话框*/
    async setRightDialog(role) {
      //存储要分配权限的角色
      this.allotRole = role
      const {data: res} = await this.$axios.get("menu/tree")
      this.authList = res.data
      this.defKeys = []
      this.getLeafKeysId(role, this.defKeys)
      this.rightDialogVisible = true
    },
    //获取当前某个角色下的所有权限的id(递归)
    getLeafKeysId(node, arr) {
      //如果循环到三级节点，就停止循环
      if (node.orderid === 3) {
        return arr.push(node.id)
      }
      //如果是一级或者二级则继续递归
      node.child.forEach(n => {
        this.getLeafKeysId(n, arr)
      })
    },
    //给角色分配新的权限
    async allotRights() {
      const keys = [...this.$refs.treeRef.getHalfCheckedKeys(), ...this.$refs.treeRef.getCheckedKeys()]
      const condition = {
        roleId: this.allotRole.id,
        menuIds: keys
      }
      const {data: res} = await this.$axios.post("role/allotRights", condition)
      if (res.code != '0') {
        return this.$message.error("分配权限失败")
      }
      this.rightDialogVisible = false
      this.$message.success("分配权限成功");
    }
  }
}
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}

.el-table {
  margin-top: 15px;
}

.el-tag {
  margin: 10px;
}

.elrow {
  padding: 5px;
}

.backLineTop {
  border: 1px solid #eee;
}

.backLineBottom {
  border: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
  text-align: center;
}
</style>